package com.jyy.prefabricated.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jyy.prefabricated.common.AjaxResult;
import com.jyy.prefabricated.dto.Pageable;
import com.jyy.prefabricated.entity.ProjectCategory;
import com.jyy.prefabricated.service.ProjectCategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 项目分类
 * @version 1.0.0
 * @author: zjj
 */
@Slf4j
@RequestMapping("/api")
@RestController
public class ProjectCategoryController {

    @Autowired
    private ProjectCategoryService projectCategoryService;

    /**
     * 新增分类
     * @param projectCategory
     * @return
     */
    @PostMapping("/project-category")
    public AjaxResult insert(@Valid @RequestBody ProjectCategory projectCategory){
        log.info("Rest request to insert projectCategory:{}",projectCategory);
        int id = projectCategoryService.insert(projectCategory);
        ProjectCategory result = projectCategoryService.selectById(id);
        return AjaxResult.success(result);
    }

    /**
     * 修改分类
     * @param projectCategory
     * @return
     */
    @PutMapping("/project-category")
    public AjaxResult update(@Valid @RequestBody ProjectCategory projectCategory){
        log.info("Rest request to update projectCategory:{}",projectCategory);
        projectCategoryService.update(projectCategory);
        ProjectCategory result = projectCategoryService.selectById(projectCategory.getId());
        return AjaxResult.success(result);
    }

    /**
     * 查询分类详情
     * @param id
     * @return
     */
    @GetMapping("/project-category/{id}")
    public AjaxResult selectById(@PathVariable Integer id){
        log.info("Rest request to find projectCategory by id:{}",id);
        ProjectCategory result = projectCategoryService.selectById(id);
        return AjaxResult.success(result);
    }

    /**
     * 删除分类
     * @param id
     * @return
     */
    @DeleteMapping("/project-category/{id}")
    public AjaxResult deleteById(@PathVariable Integer id){
        log.info("Rest request to delete projectCategory by id:{}",id);
        int row = projectCategoryService.delete(id);
        if(row > 0){
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
        }
    }

    /**
     * 分页查询分类列表
     * @param category 名称
     * @param pid 父级id
     * @param pageable 分页信息
     * @return
     */
    @GetMapping("/project-category")
    public AjaxResult selectByParams(@RequestParam(required = false,defaultValue = "") String category,@RequestParam(required = false) Integer pid, Pageable pageable){
        log.info("Rest request to find projectCategory by category:{},pid:{}",category,pid);
        PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<ProjectCategory> categories = projectCategoryService.selectByParams(category, pid);
        PageInfo<ProjectCategory> result = new PageInfo<>(categories);
        return AjaxResult.success(result);
    }

    /**
     * 分页查询分类列表
     * @param category 名称
     * @param pid 父级id
     * @return
     */
    @GetMapping("/project-category-list")
    public AjaxResult selectByParams(@RequestParam(required = false,defaultValue = "") String category,@RequestParam(required = false) Integer pid){
        log.info("Rest request to find projectCategory by category:{},pid:{}",category,pid);
        List<ProjectCategory> result = projectCategoryService.selectByParams(category, pid);
        return AjaxResult.success(result);
    }
}
